/*
 * DragHandler.java
 *
 * Created on August 30, 2007, 7:56 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.afaker.rss.gui.dnd;

import com.afaker.rss.*;
import java.awt.Point;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragGestureRecognizer;
import java.awt.dnd.DragSource;
import java.awt.dnd.DragSourceDragEvent;
import java.awt.dnd.DragSourceDropEvent;
import java.awt.dnd.DragSourceEvent;
import java.awt.dnd.DragSourceListener;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

/**
 *
 * @author bruce
 */
public class DragHandler implements DragSourceListener,DragGestureListener{
    private JTree tree;
    private DragSource source;
    private DragGestureRecognizer recognizer;
    private DefaultMutableTreeNode oldNode;
    /** Creates a new instance of DragHandler */
    public DragHandler(JTree tree) {
        this.tree = tree;
        source = new DragSource();
        recognizer = source.createDefaultDragGestureRecognizer(tree,DnDConstants.ACTION_COPY_OR_MOVE,this);
    }
   
    public void dragEnter(DragSourceDragEvent dsde) {        
    }

    public void dragOver(DragSourceDragEvent dsde) {
    }

    public void dropActionChanged(DragSourceDragEvent dsde) {
    }

    public void dragExit(DragSourceEvent dse) {
    }

    public void dragDropEnd(DragSourceDropEvent dsde) {
        tree.repaint();  
    }

    public void dragGestureRecognized(DragGestureEvent dge) {
        Point p = dge.getDragOrigin();        
        TreePath path = tree.getPathForLocation(p.x,p.y);
        if(path == null || (path.getPathCount() <= 1))
            return ;
        oldNode = (DefaultMutableTreeNode) path.getLastPathComponent();
        Transferable trans = new TransferableObject(oldNode);
        source.startDrag(dge,DragSource.DefaultCopyDrop,trans,this);
    }
    
}
